package com.eascs.ticket.ticket.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.entity.StatusConstant;
import com.eascs.ticket.configuration.entity.Attachment;
import com.eascs.ticket.configuration.service.AttachmentService;
import com.eascs.ticket.ticket.entity.TicketAttachment;
import com.eascs.ticket.ticket.entity.TicketAttachmentDelete;
import com.eascs.ticket.ticket.form.GetEditViewForm;
import com.eascs.ticket.ticket.form.SaveTicketAttachmentForm;
import com.eascs.ticket.ticket.form.TicketAttachmentForm;
import com.eascs.ticket.ticket.service.TicketAttachmentService;
import com.eascs.web.sso.NoPermissionException;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@RestController
public class TicketAttachmentRestController extends BaseRestController {

	@Inject
	private TicketAttachmentService ticketAttachmentService;

	@Inject
	private AttachmentService attachmentService;

	@RequestMapping(value = "/ticket/opt/getAttachmentEditView")
	public HtmlRestResult getAttachmentEditView(GetEditViewForm getEditViewForm) {
		TicketAttachment ticketAttachment = new TicketAttachment();
		ticketAttachment.setTicketId(getEditViewForm.getTicketId());
		ViewContext.put("ticketAttachment", ticketAttachment);
		return HtmlRestResult.popHtmlFragment("/ticket/opt/getAttachmentEditView");
	}

	@RequestMapping(value = "/ticket/opt/uploadTicketAttachment")
	public HtmlRestResult uploadTicketAttachment(@RequestParam(value = "file", required = true) MultipartFile file) {
		Attachment attachment = attachmentService.uploadAttachment(file);
		ViewContext.put("attachment", attachment);
		String view = "ticket/opt/uploadTicketAttachment";
		ViewContext.put("showImage", attachment.getName().toLowerCase().matches(".*\\.(png|gif|bmp|jpg)$"));
		return HtmlRestResult.html(view);
	}

	@RequestMapping(value = "/ticket/opt/saveTicketAttachment")
	public BaseRestResult<String> saveTicketAttachment(SaveTicketAttachmentForm saveTicketAttachmentForm) {
		TicketAttachment ticketAttachment = new TicketAttachment();
		ticketAttachment.setAttachmentId(saveTicketAttachmentForm.getAttachmentId());
		ticketAttachment.setTicketId(saveTicketAttachmentForm.getTicketId());
		ticketAttachment.setStatus(StatusConstant.STATUS_OK);
		EntityUtil.initInsertEntity(ticketAttachment);
		this.ticketAttachmentService.insertTicketAttachment(ticketAttachment);
		return BaseRestResult.refresh();
	}

	@RequestMapping(value = "/ticket/opt/deleteTicketAttachment")
	public BaseRestResult<String> deleteTicketWatcher(TicketAttachmentForm ticketAttachmentForm) {
		checkPermission(ticketAttachmentForm.getId());
		TicketAttachmentDelete ticketAttachmentDelete = new TicketAttachmentDelete();
		ticketAttachmentDelete.setId(ticketAttachmentForm.getId());
		EntityUtil.initUpdateEntity(ticketAttachmentDelete);
		this.ticketAttachmentService.deleteTicketAttachment(ticketAttachmentDelete);
		return BaseRestResult.refresh();
	}

	public void checkPermission(String id) {
		User user = UserHolder.getUser();
		if (user.isSuperAdministrator())
			return;
		TicketAttachment ticketAttachment = this.ticketAttachmentService.getTicketAttachmentById(id);
		if (user.getCode().equals(ticketAttachment.getCreator()))
			return;
		throw new NoPermissionException("您无权删除！");
	}
}
